package com.study.english.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.study.english.constant.SecurityConstants;
import com.study.english.context.SecurityContextHolder;
import com.study.english.domain.SysVideoType;
import com.study.english.pojo.vo.SysVideoVo;
import com.study.english.service.ISysVideoTypeService;
import com.study.english.util.OSSTemplate;
import com.study.english.util.VideoToImage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.ByteArrayInputStream;
import java.util.*;

import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.english.domain.SysVideo;
import com.study.english.service.ISysVideoService;
import com.study.english.mapper.SysVideoMapper;
import org.springframework.stereotype.Service;

/**
 * @author wf
 * @description 针对表【sys_video(学习视频)】的数据库操作Service实现
 * @createDate 2024-03-11 15:56:35
 */
@Service
@Slf4j
public class SysVideoServiceImpl extends ServiceImpl<SysVideoMapper, SysVideo>
        implements ISysVideoService {

    @Autowired
    private ISysVideoService sysVideoService;

    @Autowired
    private OSSTemplate ossTemplate;
    @Autowired
    private ISysVideoTypeService sysVideoTypeService;

    @Override
    public void addSysVideo(SysVideoVo sysVideoVo) {
        SysVideo sysVideo = new SysVideo();
        BeanUtils.copyProperties(sysVideoVo, sysVideo);
        sysVideo.setIsDelete(0);
        sysVideo.setCreateTime(new Date());
        sysVideoService.save(sysVideo);
    }

    @Override
    public IPage<SysVideoVo> getSysVideoPageList(IPage<SysVideoVo> page, SysVideoVo sysVideoVo) {
        IPage<SysVideoVo> sysVideoPage = this.baseMapper.getSysVideoPageList(page, sysVideoVo);
        return sysVideoPage;
    }


    @Override
    public List<SysVideoVo> getSysVideoList(SysVideoVo sysVideoVo) {
        return this.baseMapper.getSysVideoList(sysVideoVo);

    }

    @Override
    public List<SysVideoVo> getSysVideoCurrStudyList(SysVideoVo sysVideoVo) {
        return this.baseMapper.getSysVideoCurrStudyList(sysVideoVo);
    }

    @Override
    public SysVideo getSysVideoById(String id) {
        return sysVideoService.getById(id);
    }


    @Override
    public void delSysVideo(String id) {
        sysVideoService.removeById(id);
    }

    @Override
    public void addBatchSysVideo(SysVideoVo sysVideoVo) {
        List<String> fileUrlList = sysVideoVo.getFileUrlList();
        SysVideoType sysVideType = sysVideoTypeService.getById(sysVideoVo.getVideoTypeId());
        List<SysVideo> sysVideoList = new ArrayList<>();
        SysVideo sysVidoBigNum = sysVideoService.getOne(new QueryWrapper<SysVideo>().eq("video_type_id", sysVideoVo.getVideoTypeId()).orderByDesc("sort_num").last("limit 1 "));
        Integer sortNum = 10000;
        if (sysVidoBigNum != null && sysVidoBigNum.getSortNum() != null) {
            sortNum = sysVidoBigNum.getSortNum() + 100;
        }
        for (String vo : fileUrlList) {
            SysVideo sysVideo = new SysVideo();
            sysVideo.setFileUrl(vo);
            sysVideo.setStatus(0);
            sysVideo.setSortNum(sortNum);
            sysVideo.setUserId(SecurityContextHolder.getUserId());
            String fileName = FileNameUtil.mainName(vo);
            sysVideo.setFileName(fileName);
            sysVideo.setVideoTypeId(sysVideoVo.getVideoTypeId());
            sysVideo.setCameraFlag(0);
            sysVideo.setThumbnailTime(sysVideType.getThumbnailTime());
            sysVideo.setStartPlayTime(sysVideType.getStartPlayTime());
            String objectName = sysVideoService.uploadThumbnailImage(sysVideo);
            sysVideo.setThumbnailUrl("/" + objectName);
            sysVideoList.add(sysVideo);
            sortNum = sortNum + 100;
        }
        sysVideoService.saveBatch(sysVideoList);
    }

    /// 视频路径
    //sysVideo  视频
    @Override
    public String uploadThumbnailImage(SysVideo sysVideo) {
        String fileUrl = sysVideo.getFileUrl();
        String encode = URLUtil.encode(fileUrl);
        String ossHead = SecurityConstants.OSS_HTTP_HEAD + SecurityConstants.OSS_BUCKET + "." + SecurityConstants.OSS_ENDPOINT;
        sysVideo.setVideoTime(VideoToImage.videoPlayTime(ossHead + encode) / (1000 * 1000));
        ByteArrayInputStream byteArrayOutputStream = VideoToImage.videoToByteArrayOutputStreamImage(ossHead + encode, sysVideo.getThumbnailTime(), 800);
        String[] fileUrlArr = fileUrl.split("/");
        String[] fileUrlNewArr = Arrays.copyOfRange(fileUrlArr, 1, fileUrlArr.length - 1);
        String objectName = "";
        for (String s : fileUrlNewArr) {
            objectName = objectName + (objectName == "" ? s : "/" + s);
        }
        String uuid = IdUtil.simpleUUID();
        objectName = objectName + "/缩略图_" + uuid + ".jpg";
        try {
            ossTemplate.simpleInputStreamUpload(objectName, byteArrayOutputStream);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return objectName;
    }


    @Override
    public void updateSysVideo(SysVideoVo sysVideoVo) {
        SysVideo sysVideo = new SysVideo();
        BeanUtils.copyProperties(sysVideoVo, sysVideo);
        sysVideoService.updateById(sysVideo);
    }
}




